package com.online.teaching.module.common.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.online.teaching.dao.BlogMapper;
import com.online.teaching.dao.CommentMapper;
import com.online.teaching.dao.StudentMapper;
import com.online.teaching.dao.TeacherMapper;
import com.online.teaching.module.common.blog.domain.dto.BlogAddDTO;
import com.online.teaching.module.common.blog.domain.dto.CommentAddDTO;
import com.online.teaching.module.common.blog.domain.vo.BlogGetVO;
import com.online.teaching.module.common.blog.domain.vo.CommonGetVO;
import com.online.teaching.module.common.blog.service.CommonBlogService;
import com.online.teaching.pojo.Blog;
import com.online.teaching.pojo.Comment;
import com.online.teaching.utils.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName : StudentBlogServiceImpl  //类名
 * @Description :   //描述
 * @Author : YG  //作者
 * @Date: 2020-11-04 13:21  //时间
 */
@Service
public class CommonBlogServiceImpl implements CommonBlogService {


    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private CommentMapper commentMapper;


    @Override
    public PageInfo<BlogGetVO> getBlogList(int page, int size, int teachesId) {
        PageHelper.startPage(page,size);
        List<Blog> blogList = blogMapper.getBlogList(teachesId);
        List<BlogGetVO> blogGetVOS = SmartBeanUtil.copyList(blogList, BlogGetVO.class);
        return new PageInfo<>(blogGetVOS);
    }

    @Override
    public int addBlog(BlogAddDTO blogAddDTO,Integer userType,Integer id) {
        String name;
        if(userType==1){
            name=studentMapper.selectByPrimaryKey(id).getStuName();
        }else if(userType==2){
            name=teacherMapper.selectByPrimaryKey(id).getTeaName();
        }else {
            //只有老师以及学生可以发布
            return 0;
        }
        Blog blog = SmartBeanUtil.copy(blogAddDTO, Blog.class);
        blog.setPublisherName(name);
        blog.setPublisherId(id);
        int i = blogMapper.insertSelective(blog);
        return i;
    }

    @Override
    public int addBlogComment(CommentAddDTO commentAddDTO,Integer userType, Integer id) {
        String name;
        if(userType==1){
            name=studentMapper.selectByPrimaryKey(id).getStuName();
        }else if(userType==2){
            name=teacherMapper.selectByPrimaryKey(id).getTeaName();
        }else {
            //只有老师以及学生可以发布
            return 0;
        }
        Comment copy = SmartBeanUtil.copy(commentAddDTO, Comment.class);
        copy.setPublisherId(id);
        copy.setReplyPublishName(name);
        int i = commentMapper.insertSelective(copy);
        return i;
    }

    @Override
    public int deleteBlogComment(Integer userId, Integer commentId) {
        Comment comment = commentMapper.selectByPrimaryKey(commentId);
        if(comment==null||!comment.getPublisherId().equals(userId)){
            return 0;
        }
        commentMapper.updateReplyId(commentId);
        int i = commentMapper.deleteComment(commentId);
        return i;
    }

    @Override
    public int deleteBlog(Integer userId, Integer blogId) {

        Blog blog = blogMapper.selectByPrimaryKey(blogId);
        if(blog==null||!blog.getPublisherId().equals(userId)){
            return 0;
        }

        commentMapper.deleteBlog(blogId);
        int i = blogMapper.deleteBlog(blogId);
        return i;
    }

    @Override
    public List<CommonGetVO> getBlogCommentList(Integer blogId) {
        //偷懒直接用这个接口
        List<Comment> comments = commentMapper.adminGetCommentList(blogId);
        List<CommonGetVO> commonGetVOS = SmartBeanUtil.copyList(comments, CommonGetVO.class);
        return commonGetVOS;
    }
}
